package com.sun.controller;

import com.sun.config.Result;
import com.sun.entity.User;
import com.sun.service.UserService;
import org.apache.ibatis.annotations.Delete;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RequestMapping("/user")
@RestController
public class UserController {
	
	@Autowired
	private UserService uService;
	
	@GetMapping("/getUser/{id}")
	public User selectById(@PathVariable("id") Integer id){
		return  uService.selectUserById(id);
	}
	
	@PostMapping("/addUser")
	public void addUser(User user){
		uService.addUser(user);
	}

	@GetMapping("/getUserList")
	public List<User> getUserList() throws Exception {
		List<User> userList = uService.getUserList();
		return userList;
	}

	@PutMapping("/updateByid/{id}")
	public User updateByid(@PathVariable Integer id, @RequestBody User user) throws Exception {
		user.setId(id);
		return uService.updateByid(id, user);
	}

	@DeleteMapping("/deletByid/{id}")
	public void deletByid(@PathVariable Integer id){
		uService.deleteByid(id);
	}

	/**
	 * 更新用户，使用通用Mapper
	 * @param
	 * @param user
	 * @return
	 * @throws Exception
	 */
	@PutMapping("/updateUser")
	public Result updateUser(User user){
		uService.updateUser(user);
		return  Result.success(user);
	}

}